<template>
  <Card title="项目类型" :loading="loading">
    <div ref="chartRef" :style="{ width, height }"></div>
  </Card>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch } from 'vue';
  import { Card } from 'ant-design-vue';
  import { useECharts } from '/@/hooks/web/useECharts';

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '400px',
    },
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  watch(
    () => props.loading,
    () => {
      if (props.loading) {
        return;
      }
      setOptions({
        tooltip: {
          trigger: 'item',
        },
        legend: {
          bottom: '1%',
          left: 'center',
        },
        series: [
          {
            name: '项目类型',
            type: 'pie',
            radius: '70%',
            center: ['50%', '50%'],
            color: ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de'],
            data: [
              { value: 1, name: '户表改造' },
              { value: 129, name: '零星工程' },
              { value: 5, name: '数据有误' },
              { value: 22, name: '新建管道工程（含改管）' },
              { value: 10, name: '新建小区（户表）工程' },
            ],
            // .sort(function (a, b) {
            //   return a.value - b.value;
            // })
            roseType: 'radius',
            animationType: 'scale',
            animationEasing: 'exponentialInOut',
            animationDelay: function () {
              return Math.random() *400;
            },
          },
        ],
      });
    },
    { immediate: true },
  );
</script>